package com.yoomiito.app.adapter.cicle;

import android.app.Activity;
import android.net.Uri;
import androidx.annotation.Nullable;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scrat.app.selectorlibrary.activity.ImagePreviewActivity;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.cicle.CicleInfo;
import com.yoomiito.app.model.cicle.ImageItemBean;
import com.yoomiito.app.ui.fragment.my.MyCircleActivity;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.CircleImageView;
import com.yoomiito.app.widget.SpacesItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class CicleAdapter extends BaseQuickAdapter<CicleInfo.DataBean, BaseViewHolder> {

    private String pageType;
    public CicleAdapter(@Nullable List<CicleInfo.DataBean> data) {
        super(R.layout.item_cicle, data);
    }

    public CicleAdapter(@Nullable List<CicleInfo.DataBean> data,String pageType) {//我的发布专用
        super(R.layout.item_my_circle, data);
        this.pageType = pageType;
    }

    @Override
    protected void convert(final BaseViewHolder helper, CicleInfo.DataBean item) {
        if (MyCircleActivity.MyCircleAct==pageType){
            RelativeLayout view = helper.getView(R.id.item_my_circle_rl);
            
        }
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.goods_default);
        if (!TextUtils.isEmpty(item.getHeadimgurl())){
            Glide.with(App.getApplication()).load(Uri.parse(item.getHeadimgurl())).apply(requestOptions).into((CircleImageView)helper.getView(R.id.iv_photo));
        }

        helper.getView(R.id.tv_share).setVisibility(View.INVISIBLE);

        ((TextView)helper.getView(R.id.tv_name)).setText(item.getNickname());
        ((TextView)helper.getView(R.id.tv_date)).setText(""+item.getCtime());
//        ((TextView)helper.getView(R.id.tv_content)).setText(""+item.getContent());
        TextView contentTv = helper.getView(R.id.tv_content);
        contentTv.setText("" + item.getContent());
        contentTv.setOnClickListener(v -> {
            AllUtils.copyText(contentTv.getContext(),item.getContent());
            YMToast.showText("复制成功");
        });
        if ("2".equals(item.getStatus())){
            helper.getView(R.id.item_my_circle_fail).setVisibility(View.VISIBLE);
        }
        List<ImageItemBean> imageBeanList = new ArrayList<>();
        if (item.getImgurl() != null && item.getImgurl().size() > 0){
            for (String url : item.getImgurl()){
                imageBeanList.add(new ImageItemBean(url));
            }
            ImageAdapter adapter = new ImageAdapter(imageBeanList);
            RecyclerView recyclerView = (RecyclerView)helper.getView(R.id.rv_photo);

            if (recyclerView.getItemDecorationCount() >0){//避免重复添加itemDecoration
                RecyclerView.ItemDecoration itemDecoration = recyclerView.getItemDecorationAt(0);
                if(itemDecoration == null){
                    recyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dimen_6dp)));
                }
            }else{
                recyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dimen_6dp)));
            }

            recyclerView.setLayoutManager(new GridLayoutManager(mContext, Constant.GRID_IMAGE_NUM));
            recyclerView.setAdapter(adapter);
            adapter.setOnItemChildClickListener((adapter1, view, position) -> {
                ImagePreviewActivity.show((Activity) mContext, 0, imageListToArray(imageBeanList),position);
            });
        }

        if (item.getType() == Constant.PUBLISH_TYPE_GOODS){
            helper.setGone(R.id.layout_good,true);
            ((TextView)helper.getView(R.id.tv_list_title)).setText(item.getProduct_name());
            if (TextUtils.isEmpty(item.getSeller_shop_title())){
                helper.setGone(R.id.tv_store,false);
            }else {
                ((TextView)helper.getView(R.id.tv_store)).setText(item.getSeller_shop_title());
            }
            ((TextView)helper.getView(R.id.tv_money)).setText(SpannableUtils.handlePrice(item.getPrice()));
            if (!TextUtils.isEmpty(item.getPicture())){
                Glide.with(App.getApplication()).load(Uri.parse(item.getPicture())).apply(requestOptions).into((ImageView) helper.getView(R.id.iv_list));
            }
            helper.addOnClickListener(R.id.item_circle_goods_buy);

        }else{
            helper.setGone(R.id.layout_good,false);
        }
    }

    private ArrayList<String> imageListToArray(List<ImageItemBean> imageItemBeans){
        ArrayList<String> paths = new ArrayList<>();
        for (int i = 0;i< imageItemBeans.size();i++){
            paths.add(imageItemBeans.get(i).getUrl());
        }
        return paths;
    }
}
